<?php
// https://code.google.com/apis/console/?hl=ja#project:794518427170:access
// https://www.google.com/accounts/ManageDomains
//http://code.google.com/p/google-api-php-client/wiki/Samples
session_start();
require_once dirname(__FILE__).'/Google/Client.php';
require_once dirname(__FILE__).'/Google/Service/YouTube.php';

$redirect = "http://".$_SERVER["HTTP_HOST"]."/Index/oauth";
require_once 'Zend/Loader.php';
$frontController = Zend_Controller_Front::getInstance();
$appConfig = $frontController->getParam('bootstrap')->getOptions();




$client = new Google_Client();
$client->setClientId($appConfig['clientId']);
$client->setClientSecret($appConfig['clientSecret']);
//$client->setScopes('https://www.googleapis.com/auth/youtube');

$scopes=array('https://www.googleapis.com/auth/userinfo.profile','https://www.googleapis.com/auth/youtube','https://www.googleapis.com/auth/userinfo.email');
$client->setScopes($scopes);
$client->setAccessType('offline'); // default: offline
$client->setApplicationName($appConfig['applicationName']);
$client->setDeveloperKey($appConfig['developerKey']); // API key
$client->setApprovalPrompt("force");
$client->setRedirectUri($redirect);



$login_link="<div id='login'><a href='/Index/oauth'> Time out.please Login </a></div>";


$service = new Google_Service_YouTube($client);

// k.kodama
if (isset($_GET['logout'])) { // logout: destroy token
    unset($_SESSION['token']);
    unset($_SESSION['sessionToken']);
    unset($_SESSION['feed_url']);
    header('Location: /Index/logout'); 
    die('');
}


if (isset($_GET['code'])) { 
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']);
}


if (!$client->getAccessToken()) { // auth call to google
    $authUrl = $client->createAuthUrl();
  //   echo "hello001";
    header("Location: ".$authUrl);
    die;
}
if($client->getAccessToken() ){
    $_SESSION['sessionToken']=$client->getAccessToken();

    //$url = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=".$_SESSION['sessionToken'];
    //$me = json_decode(file_get_contents($url));
    //$_SESSION['me'] = $me;
    //var_dump($me);
    //exit;
   // $oauth2 = new Google_Oauth2Service($client);
   // $user = $oauth2->userinfo->get();
   // $_SESSION['me']=$user;
    
    $google_token= json_decode($_SESSION['sessionToken']);
    $client->refreshToken($google_token->refresh_token);
    //print_r($_SESSION);
    //exit;
  header('Location: /Index/'); 
}else{
    echo "cant get accsess_token";
    
}

//$_SESSION['sessionToken']=$_SESSION['token']['access_token'];
//print("<hr>1:REQUEST<hr>");
//print_r($_REQUEST);
//print("<hr>2:SESSION<hr>");
//print_r($_SESSION);
//print ("<hr>3 _SSESSION['token']<hr>");
//print_r($_SESSION['token']);
//print ("<hr/>4<hr/>");
//echo ($_SESSION['token']['access_token']);
//exit;
//$_SESSION['token']=$_SESSION['token']
//header('Location: /Index/index?'); 
